﻿using System.Diagnostics;
using static FTIOT_CAT1.Form1;

namespace FTIOT_CAT1
{
    public partial class SerialSettingFrom : Form
    {
        public static class SerialPortIndex
        {
            public static int baudRateComboBox = 3; // 默认选择9600波特率
            public static int dataBitsComboBox = 3; // 默认选择8个数据位
            public static int stopBitsComboBox = 0;// 默认选择1个停止位
            public static int parityBitsComboBox = 0;// 默认选择无校验位
        }
        public SerialSettingFrom()
        {
            InitializeComponent();
            baudRateComboBox.SelectedIndex = SerialPortIndex.baudRateComboBox; // 默认选择9600波特率
            dataBitsComboBox.SelectedIndex = SerialPortIndex.dataBitsComboBox; // 默认选择8个数据位
            stopBitsComboBox.SelectedIndex = SerialPortIndex.stopBitsComboBox; // 默认选择1个停止位
            parityBitsComboBox.SelectedIndex = SerialPortIndex.parityBitsComboBox; // 默认选择无校验位
        }

        private void ConfirmButtonClick(object sender, EventArgs e)
        {
            try
            {
                // 波特率验证与转换
                if (baudRateComboBox.SelectedItem == null)
                    throw new ArgumentNullException("未选择波特率");

                if (!int.TryParse(baudRateComboBox.SelectedItem.ToString(), out int baudRate))
                    throw new ArgumentException("无效的波特率值");

                SerialPortGlobalData.BAUDRATE = baudRate;

                // 数据位验证与转换
                if (dataBitsComboBox.SelectedItem == null)
                    throw new ArgumentNullException("未选择数据位");

                if (!int.TryParse(dataBitsComboBox.SelectedItem.ToString(), out int dataBits))
                    throw new ArgumentException("无效的数据位值");

                SerialPortGlobalData.DATABITS = dataBits;

                // 停止位验证与转换
                if (stopBitsComboBox.SelectedItem == null)
                    throw new ArgumentNullException("未选择停止位");

                // 使用明确的空值检查和非空断言操作符
                string stopBitsStr = stopBitsComboBox.SelectedItem?.ToString() ?? throw new InvalidOperationException("停止位值为空");
                SerialPortGlobalData.STOPBITS = stopBitsStr switch
                {
                    "1" => System.IO.Ports.StopBits.One,
                    "1.5" => System.IO.Ports.StopBits.OnePointFive,
                    "2" => System.IO.Ports.StopBits.Two,
                    _ => throw new ArgumentException("无效的停止位值")
                };

                // 校验位验证与转换
                if (parityBitsComboBox.SelectedItem == null)
                    throw new ArgumentNullException("未选择校验位");

                // 使用明确的空值检查和非空断言操作符
                string parityStr = parityBitsComboBox.SelectedItem?.ToString() ?? throw new InvalidOperationException("校验位值为空");
                SerialPortGlobalData.PARITYBITS = parityStr switch
                {
                    "None" => System.IO.Ports.Parity.None,
                    "Odd" => System.IO.Ports.Parity.Odd,
                    "Even" => System.IO.Ports.Parity.Even,
                    "Mark" => System.IO.Ports.Parity.Mark,
                    "Space" => System.IO.Ports.Parity.Space,
                    _ => throw new ArgumentException("无效的校验位值")
                };
                SerialPortIndex.baudRateComboBox = baudRateComboBox.SelectedIndex;
                SerialPortIndex.dataBitsComboBox = dataBitsComboBox.SelectedIndex;
                SerialPortIndex.stopBitsComboBox = stopBitsComboBox.SelectedIndex;
                SerialPortIndex.parityBitsComboBox = parityBitsComboBox.SelectedIndex;
                this.Close();
                Debug.WriteLine("修改串口参数成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"参数设置错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Debug.WriteLine($"串口参数设置失败: {ex}");
            }
        }
    }
}
